package com.xuecheng.learning.service;

import com.xuecheng.framework.domain.learning.response.GetMediaResult;
import com.xuecheng.framework.domain.search.TeachplanMediaSearch;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.learning.feign.CourseMediaSearch;

import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;

@Service
public class LearningService {
    // Feign远程调用ES搜索服务对象
    @Resource
    private CourseMediaSearch courseMediaSearch;
    /**
     * 根据课程id查询视频URL
     *
     * @param courseId    课程ID
     * @param teachplanId 某个节点的Id
     * @return 结果
     */
    public GetMediaResult getMedia(String courseId, String teachplanId) {
        // 校验是否有权限获取


        // 远程调用ES搜索服务中的查询接口,查询课程 节点 视频URL
        QueryResponseResult<TeachplanMediaSearch> result = courseMediaSearch.getTeachplanMediaByTeachplanId(teachplanId);
        if (!result.isSuccess()){
            return new GetMediaResult(CommonCode.FAIL,null);
        }else {
            QueryResult<TeachplanMediaSearch> queryResult = result.getQueryResult();
            if (queryResult == null || queryResult.getTotal() <=0 ){
                return new GetMediaResult(CommonCode.FAIL,"获取视频URL为空");
            }else {
                List<TeachplanMediaSearch> list = queryResult.getList();
                return new GetMediaResult(CommonCode.SUCCESS,list.get(0).getMediaurl());
            }
        }
    }
}
